package co.baiku.boot;

import co.baiku.boot.cache.service.CacheService;
import co.baiku.boot.cache.type.CacheEngine;
import co.baiku.boot.common.spring.SpringContext;

import java.util.HashMap;
import java.util.Map;

/**
 * 缓存
 */
public class AjavaerCache {

    public static final AjavaerCache ME = new AjavaerCache();

    private static final Map<CacheEngine, CacheService> ENGINES = new HashMap<>();

    public void init() {
        Map<String, CacheService> beansOfType = SpringContext.getBeansOfType(CacheService.class);
        beansOfType.forEach((k, v) -> ENGINES.put(v.getCacheEngine(), v));
    }

    /**
     * 获取redis
     *
     * @return
     */
    public static CacheService redis() {
        return engine(CacheEngine.redis);
    }

    /**
     * 获得caffeine
     *
     * @return
     */
    public static CacheService caffeine() {
        return engine(CacheEngine.caffeine);
    }

    /**
     * 获取指定缓存
     *
     * @param engine 指定缓存
     * @return
     */
    public static CacheService engine(CacheEngine engine) {
        if (ENGINES.isEmpty()) {
            ME.init();
        }
        return ENGINES.get(engine);
    }


}
